package com.baomidou.springwind.algorithm;

import com.baomidou.springwind.entity.NetworkNode;
import com.baomidou.springwind.pojo.CityTransportGroup;

import java.math.BigDecimal;
import java.util.*;

public class CityTransportSimulate {

    private int batch = 2;
    private int fullLoad = 15;
    private int shelf = 500 * 22;
    private NetworkNode factoryNode;

    public void simu(CityTransportGroup group) {
        System.out.println("-------中心象限-1------");
        handle(group.getCenterGroup());
        System.out.println("-------第一象限-------");
        handle(group.getFirstGroup());
        System.out.println("-------第一象限-------");
        System.out.println("-------第二象限-------");
        handle(group.getSecondGroup());
        System.out.println("-------第二象限-------");
        System.out.println("-------第三象限-------");
        handle(group.getThirdGroup());
        System.out.println("-------第三象限-------");
        System.out.println("-------第四象限-------");
        handle(group.getFourthGroup());
        System.out.println("-------第四象限-------");
        for (NetworkNode other : group.getOtherGroup()) {
            System.out.println("-------其他-------");
            handle(Arrays.asList(other));
            System.out.println("-------其他-------");
        }
    }

    private void handle(List<NetworkNode> nodes) {
        TreeSet<NetworkNode> sortedNodes = new TreeSet<NetworkNode>(new Comparator<NetworkNode>() {
            @Override
            public int compare(NetworkNode o1, NetworkNode o2) {
                return o2.getAmount().compareTo(o1.getAmount());
            }
        });
        sortedNodes.addAll(nodes);
        BigDecimal totaAmount = new BigDecimal(0);
        for (NetworkNode node : nodes) {
            totaAmount = totaAmount.add(node.getAmount());
        }
        int vehicle = (int) Math.ceil(totaAmount.doubleValue() / shelf / fullLoad / batch); //车辆数
        System.out.println("需要车辆：" + vehicle);
        BigDecimal full = new BigDecimal(vehicle * shelf * fullLoad);
        int flag = 0;
        int batchcount = batch;
        while (batchcount-- > 0) {
            BigDecimal vehicleAmount = new BigDecimal(0);
            while (sortedNodes.size() > 0) {
                NetworkNode nextNode = sortedNodes.first();
                if (vehicleAmount.add(nextNode.getAmount()).compareTo(full) >= 0) {
                    System.out.println("批次：" + batchcount + " 需求点名称：" + nextNode.getNodeName() + "运载了:" + full.subtract(vehicleAmount));
                    nextNode.setAmount(nextNode.getAmount().subtract(full.subtract(vehicleAmount)));
                    if (nextNode.getAmount().intValue() == 0) {
                        sortedNodes.pollFirst();
                    }
                    break;
                }
                System.out.println("批次：" + batchcount + " 需求点名称：" + nextNode.getNodeName() + "运载了:" + nextNode.getAmount());
                vehicleAmount = vehicleAmount.add(nextNode.getAmount());
                sortedNodes.pollFirst();
                if (flag++ > 100) {
                    return;
                }
            }
        }
    }
}
